package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.redis.RedisConst;
import com.wwu.common.redis.RedisUtil;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.Redis;
import com.wwu.system.service.IRedisService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 缓存监控前端控制器
 * @author 一蓑烟雨
 * @version 1.0.0
 * @date 2023-07-10 10:31
 **/
@Api(tags = "缓存监控控制器(RedisController)")
@ApiSort(1)
@Controller
@RequestMapping("/system/redis")
public class RedisController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private IRedisService redisService;

    /**
     * 请求转发到缓存主页
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/11 10:46
     */
    @GetMapping("/redisMain")
    public String redisMainHtml() {
        return "system/redis/redis-main";
    }

    /**
     * 请求转发到缓存新增/更新主页
     * @param redisKey
     * @param model
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/21 8:35
     */
    @GetMapping("/redisAddOrUpdate")
    public String redisAddOrUpdateHtml(String redisKey, Model model){
        if(StringUtils.hasText(redisKey)){
            Redis redis = new Redis();
            redis.setKey(redisKey);
            redis.setValue(String.valueOf(redisUtil.getValue(redisKey)));
            redis.setExpire(redisUtil.getExpire(redisKey));
            model.addAttribute("redis",redis);
        }

        return "system/redis/redis-add-edit";
    }

    @ApiOperation(value = "查询缓存信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "redisKey", value = "缓存键前缀"),
            @ApiImplicitParam(name = "redisType", value = "缓存类型")
    })
    @GetMapping("/redisList")
    @ResponseBody
    public ResultPage<Redis> redisList(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "5000") Integer limit,
                                       String redisKey,
                                       String redisType){
        return redisService.getRedisListPage(page,limit,redisKey,redisType);
    }

    @ApiOperation(value = "新增或更新缓存信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveOrUpdateRedis")
    @ResponseBody
    public  ResultObj saveOrUpdateRedis(@RequestBody Redis redis){
        redisService.saveOrUpdateRedis(redis);
        return ResultObj.success("新增或更新缓存信息");
    }

    @ApiOperation(value = " 根据缓存key批量刷新内置缓存信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParam(name = "redisKeys", value = "缓存key集合，以逗号分隔")
    @PutMapping("/updateRedisByKeys")
    @ResponseBody
    public ResultObj updateRedisByKeys(String redisKeys){
        List<String> redisKeyList =  new ArrayList<>();
        if(StringUtils.hasText(redisKeys)){
            redisKeyList = Arrays.asList(redisKeys.split(CommConst.SPLIT_COMMA));
        }else{
            //加载内置缓存
            redisKeyList.add(RedisConst.Key.SYS_DICT);
            redisKeyList.add(RedisConst.Key.SYS_CONFIG);
            redisKeyList.add(RedisConst.Key.SYS_PARAM);
        }
        redisService.updateRedisByKeys(redisKeyList);

        return ResultObj.success("缓存批量刷新成功");
    }

    @ApiOperation(value = "删除缓存信息")
    @ApiOperationSupport(order = 5)
    @DeleteMapping("/deleteRedis/{redisKeys}")
    @ResponseBody
    public ResultObj deleteRedis(@ApiParam(name = "redisKeys", value = "缓存key集合，以逗号分隔", required = true)
                                     @PathVariable String redisKeys){
        redisService.deleteRedis(Arrays.asList(redisKeys.split(CommConst.SPLIT_COMMA)));
        return ResultObj.success("缓存删除成功");
    }
}
